﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 全局数据
/// </summary>
public class GlobalData : MonoBehaviour {

    #region 单例模式
    private static GlobalData _instance;
    public static GlobalData Instance {
        get {
            if (_instance == null) {
                _instance = GameObject.Find("GlobalData").GetComponent<GlobalData>();
            }
            return _instance;
        }
    }
    #endregion

    /// <summary>
    /// 门派字典
    /// </summary>
    public Dictionary<string, Sect> SectDictionary { get; set; }
    /// <summary>
    /// 武学字典
    /// </summary>
    public Dictionary<string, Wuxue> WuxueDictionary { get; set; }
    /// <summary>
    /// 物品模板字典
    /// </summary>
    public Dictionary<string, ArticleTemplet> ArticleTempletDictionary { get; set; }

    /// <summary>
    /// 存档序数
    /// </summary>
    public int ArchiveIndex;
    /// <summary>
    /// 游戏难度
    /// </summary>
    public int Difficulty;
    /// <summary>
    /// 游戏速度
    /// </summary>
    public int GameSpeed;
    /// <summary>
    /// 场景名称
    /// </summary>
    public string SceneName;

    /// <summary>
    /// 我们的队伍
    /// </summary>
    public List<Personage> OurTeam;
    /// <summary>
    /// 我们的上场队伍
    /// </summary>
    public List<Personage> OurFightTeam;
    /// <summary>
    /// 敌人的队伍
    /// </summary>
    public List<Personage> EnemyTeam;
    /// <summary>
    /// 敌人的上场队伍
    /// </summary>
    public List<Personage> EnemyFightTeam;
    /// <summary>
    /// 战斗武学组
    /// </summary>
    public List<FightWuxueGroup> FightWuxueGroups;
    /// <summary>
    /// 使用的战斗武学组序数
    /// </summary>
    public int FightWuxueGroupIndex;
    /// <summary>
    /// 战斗药品
    /// </summary>
    public List<ArticleInfo> FightDrugs;
    /// <summary>
    /// 是否正在逃跑
    /// </summary>
    public bool IsEscaping;

    /// <summary>
    /// 各个场景的人物位置信息
    /// </summary>
    public Dictionary<string, Dictionary<string, ModelInfo>> ScenePersonageInfo;
    /// <summary>
    /// 人物位置
    /// </summary>
    //public Dictionary<string, ModelInfo> PersonageInfo { get; set; }
    /// <summary>
    /// 触发战斗的模型名
    /// </summary>
    public string TriggerName;

    public const string ArchiveIndexLabel = "ArchiveIndex";
    public const string DifficultyLabel = "Difficulty";
    public const string GameSpeedLabel = "GameSpeed";
    public const string SceneNameLabel = "SceneName";
    public const string OurTeamLabel = "OurTeam";
    public const string OurFightTeamLabel = "OurFightTeam";
    public const string FightWuxueGroupsLabel = "FightWuxueGroups";
    public const string FightWuxueGroupIndexLabel = "FightWuxueGroupIndex";
    public const string FightDrugsLabel = "FightDrugs";
    public const string ScenePersonageInfoLabel = "ScenePersonageInfo";


    private void Awake() {
        SectDictionary = JsonHelper.LoadObject<Dictionary<string, Sect>>("Data/sects");
        WuxueDictionary = JsonHelper.LoadObject<Dictionary<string, Wuxue>>("Data/wuxues");
        ArticleTempletDictionary = JsonHelper.LoadObject<Dictionary<string, ArticleTemplet>>("Data/article_templets");

        // Debug.Log(wuxues[0].WuxueGrades[0].Attack);
        // Debug.Log(WuxueDictionary["b8101d5cc54d4c4981000a481f79cfa8"].WuxueGrades[0].Attack);
    }

    // Use this for initialization
    void Start () {
        GameObject.DontDestroyOnLoad(this);
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    /// <summary>
    /// 获取主角信息
    /// </summary>
    /// <returns></returns>
    public Personage GetPlayerPersonage() {
        return this.OurTeam[0];
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    public void SaveData() {
        DataHelper.SaveData(ArchiveIndexLabel, this.ArchiveIndex);
        DataHelper.SaveData(DifficultyLabel + this.ArchiveIndex, this.Difficulty);
        DataHelper.SaveData(GameSpeedLabel + this.ArchiveIndex, this.GameSpeed);
        DataHelper.SaveData(SceneNameLabel + this.ArchiveIndex, this.SceneName);
        DataHelper.SaveData(OurTeamLabel + this.ArchiveIndex, this.OurTeam);
        DataHelper.SaveData(OurFightTeamLabel + this.ArchiveIndex, this.OurFightTeam);
        DataHelper.SaveData(FightWuxueGroupsLabel + this.ArchiveIndex, this.FightWuxueGroups);
        DataHelper.SaveData(FightWuxueGroupIndexLabel + this.ArchiveIndex, this.FightWuxueGroupIndex);
        DataHelper.SaveData(FightDrugsLabel + this.ArchiveIndex, this.FightDrugs);
        DataHelper.SaveData(ScenePersonageInfoLabel + this.ArchiveIndex, this.ScenePersonageInfo);
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    public void LoadData(int archiveIndex) {
        this.ArchiveIndex = archiveIndex;
        this.Difficulty = DataHelper.LoadIntData(DifficultyLabel + archiveIndex);
        this.GameSpeed = DataHelper.LoadIntData(GameSpeedLabel + archiveIndex);
        this.SceneName = DataHelper.LoadStringData(SceneNameLabel + archiveIndex);
        this.OurTeam = DataHelper.LoadListData<List<Personage>>(OurTeamLabel + archiveIndex);
        this.OurFightTeam = DataHelper.LoadListData<List<Personage>>(OurFightTeamLabel + archiveIndex);
        this.FightWuxueGroups = DataHelper.LoadListData<List<FightWuxueGroup>>(FightWuxueGroupsLabel + archiveIndex);
        this.FightWuxueGroupIndex = DataHelper.LoadIntData(FightWuxueGroupIndexLabel + archiveIndex);
        this.FightDrugs = DataHelper.LoadListData<List<ArticleInfo>>(FightDrugsLabel + archiveIndex);
        this.ScenePersonageInfo = DataHelper.LoadObjectData<Dictionary<string, Dictionary<string, ModelInfo>>>(ScenePersonageInfoLabel + archiveIndex);
    }

    public void InitData() {
        Personage p = InstancePlayer();
        this.ArchiveIndex = 0;
        this.Difficulty = 0;
        this.GameSpeed = 1;
        this.SceneName = "Init";
        this.OurTeam = new List<Personage>();
        this.OurTeam.Add(p);
        this.OurFightTeam = new List<Personage>();
        this.OurFightTeam.Add(p);
        this.FightWuxueGroups = new List<FightWuxueGroup>();
        for (int i = 0; i < 3; i++) {
            this.FightWuxueGroups.Add(new FightWuxueGroup());
        }
        this.FightWuxueGroupIndex = 0;
        this.FightDrugs = new List<ArticleInfo>();
        for (int i = 0; i < 6; i++) {
            this.FightDrugs.Add(new ArticleInfo());
        }
        string data = Resources.Load<TextAsset>("Data/init").ToString();
        this.ScenePersonageInfo = new Dictionary<string, Dictionary<string, ModelInfo>>();
        this.ScenePersonageInfo.Add("Init", JsonHelper.ParseObject<Dictionary<string, ModelInfo>>(data));
        this.ScenePersonageInfo.Add("HugeMap", JsonHelper.ParseObject<Dictionary<string, ModelInfo>>(data));
    }

    private Personage InstancePlayer() {
        Personage p = PersonageManager.initPersonage("王", "小明");
        p.CurrentAttack += 5;
        //p.CurrentDefense += 20;
        return p;
    }
}
